package com.cdks.wuxiu.api.service;

import com.cdks.wuxiu.api.model.dto.UserScoreDTO;
import com.cdks.wuxiu.api.model.dto.WalletInfoDTO;
import com.cdks.wuxiu.api.service.mysql.UserBillInfoService;
import com.cdks.wuxiu.api.service.mysql.UserScoreInfoService;
import com.cdks.wuxiu.api.service.mysql.UserWalletInfoService;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.common.component.constant.ApiCodeEnum;
import com.cdks.wuxiu.common.exception.ApiException;
import com.cdks.wuxiu.entity.mysql.UserBillInfo;
import com.cdks.wuxiu.entity.mysql.UserWalletInfo;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;


/**
 * 钱包服务实现类
 */
@Service
public class WalletServiceImpl implements WalletService {
    @Resource
    private UserWalletInfoService userWalletInfoService;
    @Resource
    private UserBillInfoService userBillInfoService;
    @Resource
    private UserScoreInfoService userScoreInfoService;     


    @Override
    public UserWalletInfo getUserWalletNoScore(long userId) {
        UserWalletInfo wallet  = userWalletInfoService.getByUserId(userId);
        if (wallet == null)
        {
            throw new ApiException(ApiCodeEnum.ACCOUNT_ERROR);
        }

        return wallet;
    }

    @Override
    public WalletInfoDTO getUserWalletWithScore(long userId, long expire) {
        WalletInfoDTO wallet  = userWalletInfoService.getInfoWithScore(userId, expire);
        if (wallet == null)
        {
            throw new ApiException(ApiCodeEnum.ACCOUNT_ERROR);
        }

        return wallet;
    }

    @Override
    public long getBeanBalance(long userId) {
        UserWalletInfo wallet  = userWalletInfoService.getByUserId(userId);
        if (wallet == null)
        {
            throw new ApiException(ApiCodeEnum.ACCOUNT_ERROR);
        }

        return wallet.getBean();
    }

    @Override
    public long getCoinBalance(long userId) {
        UserWalletInfo wallet  = userWalletInfoService.getByUserId(userId);
        if (wallet == null)
        {
            throw new ApiException(ApiCodeEnum.ACCOUNT_ERROR);
        }

        return wallet.getCoin();
    }

    @Override
    public long getScoreBalance(long userId) {
        UserScoreDTO score  = userScoreInfoService.getBalanceByUser(userId, TimeUtils.getTimeSecond());

        return score.getScore();
    }

    @Override
    public List<UserBillInfo> getBill(long userId) {
        return getBill(userId, 0, 0, TimeUtils.getTimeSecond());
    }

    @Override
    public List<UserBillInfo> getBill(long userId, int page, long start, long stop) {
        return userBillInfoService.getByUserId(userId, page, start, stop);
    }   
}
